import gobject

class Observable:
    def __init__(self):
        #eventhandler is a key = even, eventfuntion is a list of event functions
        self._eventhandler = {}
        
    def attach(self, event, eventfunction):
        """attaches new eventhandler function to a event"""
        if self._eventhandler == None:
            self._eventhandler = {}
        if self._eventhandler.has_key(event):
            self._eventhandler[event].append(eventfunction)
        else:
            self._eventhandler[event] = [eventfunction]
            
    def detach(self, event, eventfunction):
        """removes eventhandler function for an event"""
        try:
            self._eventhandler[event].remove(eventfunction)
        except ValueError:
            pass
     
    def notify(self, eventdict):
        """calls the eventhandler functions for an event"""
        try:
            [func(eventdict) for func in self._eventhandler[eventdict['event']]]
        except KeyError:
            pass

    def notifyg(self, eventdict):
        """calls the eventhandler functions for an event"""
        try:
            [gobject.idle_add(func, eventdict) for func in self._eventhandler[eventdict['event']]]
        except KeyError:
            pass
            
    def clear_events(self):
        self._eventhandler = None

            